﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Insert : System.Web.UI.Page
{
    String strCon;
    String strCom;
    SqlConnection objCon;
    SqlCommand objCom;
    SqlDataReader dtReader;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            ListBindData();
    }

    private void ListBindData()
    {
        strCon = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        objCon = new SqlConnection(strCon);
        objCon.Open();
        //rbl
        strCom = "Select F_ID,F_Name From NontananYe_SQLLogin_1.Food";
        objCom = new SqlCommand(strCom, objCon);

        dtReader = objCom.ExecuteReader();
        rblFood.DataSource = dtReader;
        rblFood.DataValueField = "F_ID";
        rblFood.DataTextField = "F_Name";
        rblFood.DataBind();

        dtReader.Close();
        dtReader = null;
        //cbl
        strCom = "Select C_ID,C_Name From NontananYe_SQLLogin_1.Color";
        objCom = new SqlCommand(strCom, objCon);

        dtReader = objCom.ExecuteReader();
        cblColor.DataSource = dtReader;
        cblColor.DataValueField = "C_ID";
        cblColor.DataTextField = "C_Name";
        cblColor.DataBind();

        dtReader.Close();
        dtReader = null;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        strCon = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        objCon = new SqlConnection(strCon);
        objCon.Open();
        strCom = "INSERT INTO NontananYe_SQLLogin_1.Customer (Cus_FirstName,Cus_LastName,Cus_NickName,Cus_FavoritesFood) " +
            " VALUES "
            + " ('" + txtCustomerFirstName.Text
            + "','" + txtCustomerLastName.Text
            + "','" + txtCustomerNickName.Text
            + "','" + rblFood.SelectedValue + "'); SELECT CAST(scope_identity() AS int)";
        objCom = new SqlCommand(strCom, objCon);
        try
        {
            int newCusID = 0;
            newCusID = (int)objCom.ExecuteScalar();
            foreach (ListItem item in cblColor.Items)
            {
                if (item.Selected)
                {
                    strCom = "INSERT INTO NontananYe_SQLLogin_1.Customer_Detail (CusDetail_Cus_ID, CusDetail_Color_ID) " +
                    " VALUES "
                    + " ('" + newCusID
                    + "','" + item.Value + "');";
                    objCom = new SqlCommand(strCom, objCon);
                    objCom.ExecuteNonQuery();
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        Server.Transfer("~/Select.aspx");
    }
}